<?php
namespace app\admin\controller;

use app\admin\model\Admin as AdminModel;
use app\admin\controller\Base;

class Admin extends Base
{
    public function lit()
    {
        $list = AdminModel::paginate(3);
        $this->assign('list', $list);
        return $this->fetch('lit');
    }
    public function add()
    {
        if (request()->isPost()) {
            $data['username'] = input('username');
            $data['password'] = md5(input('password'));

            $validate = validate('Admin');
            $check = $validate->check($data);
            if (!$check) {
                $this->Error($validate->getError());
                die;
            }
            $result = db('admin')->insert($data);
            if ($result) {
                $this->success('用户输入成功', 'lit');
            } else {
                $this->error('用户注册失败');
            }
        }
        return $this->fetch('add');
    }
    public function edit()
    {
        $id = input('id');
        $admins = db('admin')->where('id', $id)->find();
        if (request()->isPost()) {
            $data['username'] = input('username');
            if (input('password')) {
                $data['password'] = md5(input('password'));
            } else {
                $data['password'] = $admins['password'];
            }

            $result = db('admin')->where('id', $id)->update($data);
            if ($result) {
                $this->success('更改成功', 'lit');
            } else {
                $this->error('密码未变');
            }

        } else {
            $this->assign('admins', $admins);
            return $this->fetch('edit');
        }

    }
    public function del()
    {
        $result = db('admin')->where('id',input('id'))->delete();
        if($result){
            $this->success('删除成功','lit');
        }else{
            $this->success('删除失败','lit');
        }
    }
}
